package com.jiyun.controller;


import com.jiyun.pojo.entity.User;
import com.jiyun.pojo.vo.UserVo;
import com.jiyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 校验数据是否可用
     * @param data
     * @param type
     * @return
     */
    @GetMapping("check/{data}/{type}")
    public Boolean checkUserData(@PathVariable("data") String data, @PathVariable(value = "type") Integer type) {
        Boolean boo = this.userService.checkData(data, type);
        return boo;
    }

    /**
     * 发送手机验证码
     * @param phone
     * @return
     */
    @RequestMapping("code")
    public ResponseEntity<Void> sendVerifyCode(String phone) {
        System.out.println(phone);
        Boolean boo = this.userService.sendVerifyCode(phone);
        if (boo == null || !boo) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
    @RequestMapping("register")
    public ResponseEntity<Void> register(@RequestBody UserVo uservo) {
        System.out.println(uservo);
        String code = uservo.getCode();
        User user = new User();
        user.setUsername(uservo.getUsername());
        user.setPassword(uservo.getPassword());
        user.setPhone(uservo.getPhone());
        user.setRoleid(uservo.getRoleid());
        user.setNickname(uservo.getNickname());
        user.setMoney(1000.00);
        user.setStatus(true);
        if (user.getRoleid()==2){
            user.setUserdescription("普通用户");
        }else{
            user.setUserdescription("跑单员");
        }

        Boolean boo = this.userService.register(user, code);
        if (boo == null || !boo) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @RequestMapping("login")
    public String login(User user){
        User user1 = userService.login(user);
        if (user1!=null){
            if (user1.getRoleid() == 2){
                return "2";
            }
            if (user1.getRoleid() == 3){
                return "3";
            }
        }
        return "false";
    }

    @PostMapping("add")
    public String add(User user){
        userService.addUser(user);
        return "true";
    }

    @PostMapping("update")
    public String update(User user){
        userService.updateUser(user);
        return "true";
    }

    @DeleteMapping("delete")
    public String delete(Integer id){
        userService.delete(id);
        return "true";
    }

    @RequestMapping("query")
    public User queryUser(
            @RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        User user1 = this.userService.queryUser(username, password);
        if (user1 == null) {
            return null;
        }
        return user1;
    }
    //根据手机查询
    @RequestMapping("queryPhone")
    User queryPhone( @RequestBody UserVo userVo){
        System.out.println(userVo.getPhone());
        String phone = userVo.getPhone();
        return  userService.selectByPhone(phone);
    }
}
